Introduction
- MATLAB (matrix laboratory) is a high-level programming language used for numerical computation, visualization and programming
- MATLAB has numerous built-in commands and mathematics functions that help with mathematical calculations, generating plots, and performing numerical methods.
- MATLAB is widely used as an industry-standard computational tool in science and engineering including −
- Signal Processing and Communications
- Control Systems
- Test and Measurement
- The analysis of electrical power systems
- Mars rover vehicles, Beijing Olympics etc.
- MATLAB has been commercially available since 1984 and is now considered as a standard tool at most universities and industries worldwide.
- MATLAB Was extensively used in the Beijing Olympic games
Matlab logo
Wikimedia/ public domain
Matlab user interface
Wikimedia/ CC BY-SA 4.0
Simulink
- MATLAB has two components – the coding element described on the previous slide and a GUI function
- Simulink is the graphical user interface (GUI) of MATLAB
- It consists of a number of blocks using MATLAB code to process inputs and produce outputs
- It can be used for modelling, simulating and analysing dynamic systems such as wind turbines, transmission systems and electronic converters.
Its primary interface is a graphical block diagramming tool and a customizable set of block libraries.
Getting Started
- Open MATLAB by double-clicking on the MATLAB shortcut icon
- When you start MATLAB, the MATLAB desktop window appears containing other windows:
- Current Folder — Access your files.
- Command Window — Enter commands at the command line, indicated by the prompt (>>)
Common Matlab operators
Symbol | Operation | Example |
+ | Addition | 2+3 |
- | Subtraction | 3-2 |
* | Multiplication | 2*3 |
/ | Normal Division | 2/3 |
\ | Reverse Division | 3\2 |
^ | Exponential | 3^2 |
pi | π | Sin(pi) |
Variables
- Variables are created with an assignment statement:
- variable name = a value (or an expression)
- For example,
- >> x = expression
- where expression is a combination of numerical values, mathematical operators, variables, and functions
- Once a variable has been created, it can be reassigned.
- The intermediate results can be suppressed by putting a semicolon (;) at the end of the line.
- The sequence of commands looks like this:
- >> t = 5;
- >> t = t+1
- t = 6
Error Messages
- If we enter an expression incorrectly, MATLAB will return an error message and suggest an example, for example:
- y = 10;
- 5y
- in the above expression, the multiplication sign, *, is left out.
- The MATLAB response is shown.
- To make corrections - retype the expressions.
- But if the expression is lengthy, we make more mistakes by typing a second time.
- A previously typed command can be recalled with the up-arrow key “↑ ".
- When the command is displayed at the command prompt, it can be modified if needed and executed.
Hierarchy of Operations
Consider expressions with parentheses:
- For example, 1 + 2 x 3 will become (1 + 2) x 3
- >> (1+2)*3
- ans =
- 9
- Also,
- >> 1+2*3
- ans =
- 7
- By adding parentheses, these two expressions give different results: 9 and 7
The order in which MATLAB performs arithmetic operations is:
- Exponentiations are done first, followed by
- Multiplications and divisions, and
finally by additions and subtractions.
Comments
- Comments can be added give a program a title and explain each line but don’t appear in the output generated
- Comments should be preceded with a % sign
- Text comments in the output can be created using commas ‘text’.
Question 1
Solve the following expression in MATLAB using parenthesis as appropriate:
Question 2
Use MATLAB to calculate the following:
(410.1297, 17.1123)
Decimal Points
MATLAB by default displays only 4 decimals in the result of the calculations, for example, 7766 as shown in the previous example.
However, MATLAB does numerical calculations in 15 digits.
The command format controls how the results of computations are displayed:
- >> format short
- >> x=0.7766
If we want to see all 15 digits, we use the command format long
- >> format long
- >> x= 0.776623376623377
To return to the standard format, enter format short, or simply format.
MATLAB repeats entries after the command prompt. Sometimes this is not quite useful, in particular when the output is pages in length. To prevent MATLAB from echoing what we type, simply enter a semicolon (;) at the end of the command.
For example,
- >> x=-3.1416;
and then ask about the value of x by typing,
- >> x
- x =
- -3.1416
Managing the Workspace
To display a list of the variables currently in the memory, type
- >> who, or
- >> whos
Which will give more details of size, space allocation, and class of the variables.
Multiple Statements
It is possible to enter multiple statements per line. Use commas (,) or semicolons (;) to enter more than one statement at once. Commas (,) allow multiple statements per line without suppressing the output.
- >> a=7; b=cos(a), c=cosh(a)
- b =
- 6570
- c =
- 3170
Question 3
Solve the following expression using MATLAB:
Miscellaneous Commands
Here are few additional useful commands:
- To clear the Command Window, type clc
- To abort a MATLAB computation, type ctrl-c
- To continue a line, type . . .
- Getting help: By typing help in the command window
- The lookfor command searches the quick summary information in each function for a match, e.g.
- >> lookfor inverse
- Or to find more specific information e.g. the square root of a number:
- help sqrt
- the doc function opens the on-line version of the help manual
- >> doc plot
Summary of Commands
Command |
Function |
clc | Clears the command window. |
clear | Removes all variables from memory. |
clear var1 | Removes the variable 'var1' from memory. |
quit | Stop Matlab. |
who | Lists the variables currently in memory. |
whos | Lists the current variables and sizes and indicates if they have imaginary parts. |
: | Colon: Generates an array with regularly spaced elements |
; | Semi-colon: Suppresses screen printing, also denotes a new row in array. |
, | Comma: Separates array elements. |
... | Continue a line. |
Question 4
The volume of a circular cylinder of height h and radius r is given by:
A particular cylindrical tank is 15 m tall and has a radius of 8 m.
Calculate the radius of a cylindrical tank with a volume 20 percent greater but having the same height.
- Set the radius, height and calculate volume 1, V1
- Set V2 as V1 + 20% of V1
- Calculate r2 (r2 = 8.7636)
Volume of a cylinder:
Vol = πr2h
Complex Numbers
MATLAB handles complex number algebra automatically e.g. the number c1 = (1 - 2i) is entered as follows:
- c1 = 1-2i
You can also type
- c1 = complex(1, -2)
The phase angle, P and magnitude, M of a complex number, n is given by:
- n = 3 + 4i
- P = angle(n)
- Y = abs(n)
Complex numbers can be used with variables:
- R = 10
- X = 20
- Z = complex(R,X)
Function | Syntax | description |
abs | Y = abs(X) | Absolute value and complex magnitude |
angle | P = angle(Z) | Phase angle |
conj | ZC = conj(Z) | Complex conjugate |
imag | Y = imag(Z) | Imaginary part of complex number |
real | X = real(Z) | Real part of complex number |
Questions
Question 5
Given that a = (-5 + 9i) and b = (6 – 2i), use MATLAB to calculate:
- a + b
- a x b
- a/b
Question 6
Set the following complex number in the worksheet:
- a = 5 + 3i
Enter the following lines of MATLAB code, and describe what each MATLAB command does.
- real(a)
- imag(a)
- abs(a)
- conj(a)
Question 7
Set the following complex numbers up in the worksheet:
- a = 5 + 3i
- b = 2 - 4i
- c = - 3 + i
- d = - 2 - 4i
Enter each of the following:
- angle(a)
- angle(b)
- angle(c)
- angle(d)
and describe what the angle function does.
Polar – Rectangular Conversion
- It is often useful to represent complex numbers in polar form.
- The built-in MATLAB function "cart2pol" converts cartesian coordinates (x,y) to polar coordinates (R∟θ0).
- For example, convert the complex number a = 5 + 3i to its polar form:
- a=5+3i;
- [Angle_a, Mag_a] = cart2pol( real(a), imag(a) )
- The built-in MATLAB function "pol2cart" converts polar coordinates (Theta,R) to cartesian coordinates (x,y).
- To covert back to the original a:
- [x, y] = pol2cart(Angle_a, R_a)
Question 8
Convert the following complex numbers to polar form using MATLAB:
- a = - 3 + i
- b = - 2 - 4i
Convert the following complex numbers to rectangular (cartesian) form using MATLAB:
- c = 5∟530
- d = -4∟300
Question 9
A transformer coil with an inductance 318.3mH and negligible resistance is connected in series with a 100 Ω resistor to a 230∟00 V, 50 Hz supply.
Construct a MATLAB program to determine:
- a) the inductive reactance of the coil, XL = 2πfL Ω
- b) the impedance of the circuit, Z = (R + jXL)
- c) the current in polar form, I =
- d) convert the current phase angle to degrees
Use comments to explain each line and generate text to explain the output e.g. ‘current, amps’, ‘phase angle in degrees’ etc.
(100∟900 Ω, 141.42∟450 Ω, 1.63∟-450 A)